package com.ruoyi.gis.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.gis.domain.FuncMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gis.domain.DatasetFunction;
import com.ruoyi.gis.service.IDatasetFunctionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据集算法Controller
 *
 * @author rww
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/earthai/function")
public class DatasetFunctionController extends BaseController
{
    @Autowired
    private IDatasetFunctionService datasetFunctionService;

    /**
     * 查询数据集算法列表
     */
//    @PreAuthorize("@ss.hasPermi('earthai:function:list')")
    @GetMapping("/list")
    public TableDataInfo list(DatasetFunction datasetFunction)
    {
        startPage();
        List<DatasetFunction> list = datasetFunctionService.selectDatasetFunctionList(datasetFunction);
        return getDataTable(list);
    }
    @GetMapping("/list/all")
    public AjaxResult listAll(DatasetFunction datasetFunction)
    {
        List<DatasetFunction> list = datasetFunctionService.
                selectDatasetFunctionList(datasetFunction);
        return AjaxResult.success(list);
    }
    /**
     * 导出数据集算法列表
     */
//    @PreAuthorize("@ss.hasPermi('earthai:function:export')")
    @Log(title = "数据集算法", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DatasetFunction datasetFunction)
    {
        List<DatasetFunction> list = datasetFunctionService.selectDatasetFunctionList(datasetFunction);
        ExcelUtil<DatasetFunction> util = new ExcelUtil<DatasetFunction>(DatasetFunction.class);
        util.exportExcel(response, list, "数据集算法数据");
    }

    /**
     * 获取数据集算法详细信息
     */
//    @PreAuthorize("@ss.hasPermi('earthai:function:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(datasetFunctionService.selectDatasetFunctionById(id));
    }

    /**
     * 新增数据集算法
     */
//    @PreAuthorize("@ss.hasPermi('earthai:function:add')")
    @Log(title = "数据集算法", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DatasetFunction datasetFunction)
    {
        return toAjax(datasetFunctionService.insertDatasetFunction(datasetFunction));
    }

    /**
     * 修改数据集算法
     */
//    @PreAuthorize("@ss.hasPermi('earthai:function:edit')")
    @Log(title = "数据集算法", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DatasetFunction datasetFunction)
    {
        return toAjax(datasetFunctionService.updateDatasetFunction(datasetFunction));
    }

    /**
     * 删除数据集算法
     */
//    @PreAuthorize("@ss.hasPermi('earthai:function:remove')")
    @Log(title = "数据集算法", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(datasetFunctionService.deleteDatasetFunctionByIds(ids));
    }
    ///////////////////////////////////////
    @GetMapping(value = "/menus")
    public AjaxResult getmenus()
    {
        DatasetFunction query = new DatasetFunction();
        query.setPublish(1);//查询已发布的
        List<DatasetFunction> functions = datasetFunctionService.
                selectDatasetFunctionList(query);
        List<FuncMenu> data = new ArrayList<>();
        FuncMenu startGroupMenu = new FuncMenu();
        startGroupMenu.setType("group");
        startGroupMenu.setName("开始节点");
        startGroupMenu.setOpen(true);
        List<FuncMenu> startChildren = new ArrayList<>();
        FuncMenu start = new FuncMenu();
        start.setFun("start");
        start.setType("start");
        start.setName("数据输入");
        start.setOpen(false);
        start.setIco("el-icon-time");
        start.setConfig("[{\"code\":\"select_file\",\"funcId\":0,\"id\":0,\"name\":\"数据输入配置\",\"params\":{},\"sort\":1,\"type\":\"SelectFile\"}]");
        startChildren.add(start);
        startGroupMenu.setChildren(startChildren);
        data.add(startGroupMenu);
        if(functions!=null){
            functions.stream().collect(Collectors.groupingBy(DatasetFunction::getType))
                    .forEach((key,vals)->{
                        FuncMenu groupMenu = new FuncMenu();
                        groupMenu.setType("group");
                        groupMenu.setName(vals.get(0).getTypeName());
                        groupMenu.setOpen(true);
                        List<FuncMenu> children = new ArrayList<>();
                        vals.stream().forEach(su->{
                            FuncMenu child = new FuncMenu();
                            child.setFun(su.getId()+"");
                            child.setType(su.getCode());
                            child.setName(su.getName());
                            child.setOpen(false);
                            child.setIco(su.getIcon());
                            child.setConfig(su.getConfigs());
                            children.add(child);
                        });
                        groupMenu.setChildren(children);
                        data.add(groupMenu);
            });

        }
        FuncMenu endGroupMenu = new FuncMenu();
        endGroupMenu.setType("group");
        endGroupMenu.setName("结束节点");
        endGroupMenu.setOpen(true);
        List<FuncMenu> endChildren = new ArrayList<>();
        FuncMenu end = new FuncMenu();
        end.setFun("end");
        end.setType("end");
        end.setName("数据输出");
        end.setOpen(false);
        end.setIco("el-icon-caret-right");
        endChildren.add(end);
        endGroupMenu.setChildren(endChildren);
        data.add(endGroupMenu);
        return success(data);
    }
}
